package cn.study.zgm.nettyall.channel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * packageName cn.study.zgm.nettyall.channel
 *
 * @author 你的名字
 * @version JDK 8
 * @className TestChannelTransfer (此处以class为例)
 * @date 2025/2/24
 * @description TODO
 */
public class TestFileChannelTransfer {
    public static void main(String[] args) {
        try(
                FileChannel readChannel = new FileInputStream("E:\\temp\\CentOS-7-x86_64-DVD-2003.iso").getChannel();
                FileChannel writeChannel = new FileOutputStream("E:\\temp\\CentOS-7-x86_64-DVD-200301.iso").getChannel();
        ){
//            writeChannel.transferFrom(readChannel,0,readChannel.size());
//            readChannel.transferTo(0,readChannel.size(),writeChannel);
            // channel底层传输最大传输2GB数据，针对于大文件需要分段进行存储
            final long size = readChannel.size();
            for (long left = size;left>0;){
                System.out.println("打印position:["+readChannel.position()+"] left:["+left+"]"+" position:["+size+"]");
                left -= readChannel.transferTo(size-left, left, writeChannel);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
